package com.lv.common.im.utils;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.lv.common.BuildConfig;
import com.lv.common.base.BaseApplication;
import com.lv.common.im.push.HandleOfflinePushCallBack;
import com.lv.common.im.push.OfflineMessageBean;
import com.lv.common.im.push.OfflineMessageDispatcher;
import com.lv.common.utils.ARoutUtils;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuicore.interfaces.TUILoginConfig;


public class TUIUtils {
    public static final String TAG = TUIUtils.class.getSimpleName();

    public static void startActivity(String activityName, Bundle param) {
        TUICore.startActivity(activityName, param);
    }

    public static void startChat(String chatId, String chatName, int chatType) {
        Bundle bundle = new Bundle();
        bundle.putString(TUIConstants.TUIChat.CHAT_ID, chatId);
        bundle.putString(TUIConstants.TUIChat.CHAT_NAME, chatName);
        bundle.putInt(TUIConstants.TUIChat.CHAT_TYPE, chatType);
        if (chatType == V2TIMConversation.V2TIM_C2C) {
            TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, bundle);
        } else if (chatType == V2TIMConversation.V2TIM_GROUP) {
            TUICore.startActivity(TUIConstants.TUIChat.GROUP_CHAT_ACTIVITY_NAME, bundle);
        }

    }

//    public static boolean isZh(Context context) {
//        Locale locale;
//        if (TUIBuild.getVersionInt() < Build.VERSION_CODES.N) {
//            locale = context.getResources().getConfiguration().locale;
//        } else {
//            locale = context.getResources().getConfiguration().getLocales().get(0);
//        }
//        String language = locale.getLanguage();
//        if (language.endsWith("zh"))
//            return true;
//        else
//            return false;
//    }

    public static int getCurrentVersionCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException ignored) {
            LogUtils.e(TAG, "getCurrentVersionCode exception= " + ignored);
        }
        return 0;
    }

    public static void handleOfflinePush(Intent intent, HandleOfflinePushCallBack callBack) {
        Context context = BaseApplication.Companion.getContext().getApplicationContext();
        if (V2TIMManager.getInstance().getLoginStatus() == V2TIMManager.V2TIM_STATUS_LOGOUT) {
//            Intent intentAction = new Intent(context, Home.class);
//            if (intent != null) {
//                intentAction.putExtras(intent);
//            }
//            intentAction.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(intentAction);
            ARouter.getInstance().build(ARoutUtils.HomeActivityURL).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).navigation();
            if (callBack != null) {
                callBack.onHandleOfflinePush(false);
            }
            return;
        }

        final OfflineMessageBean bean = OfflineMessageDispatcher.parseOfflineMessage(intent);
        if (bean != null) {
            if (callBack != null) {
                callBack.onHandleOfflinePush(true);
            }
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.cancelAll();
            }

            if (bean.action == OfflineMessageBean.REDIRECT_ACTION_CHAT) {
                if (TextUtils.isEmpty(bean.sender)) {
                    return;
                }
                TUIUtils.startChat(bean.sender, bean.nickname, bean.chatType);
            }
        }
    }

    public static void handleOfflinePush(String ext, HandleOfflinePushCallBack callBack) {
        Context context = BaseApplication.Companion.getContext().getApplicationContext();
//        ToastUtils.showShort("handleOfflinePush");
//        if (V2TIMManager.getInstance().getLoginStatus() == V2TIMManager.V2TIM_STATUS_LOGOUT) {
////            Intent intentAction = new Intent(context, SplashActivity.class);
////            if (!TextUtils.isEmpty(ext)) {
////                intentAction.putExtra(TUIConstants.TUIOfflinePush.NOTIFICATION_EXT_KEY, ext);
////            }
////            intentAction.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
////            context.startActivity(intentAction);
//            ARouter.getInstance().build(ARoutUtils.HomeActivityURL).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).navigation();
//            if (callBack != null) {
//                callBack.onHandleOfflinePush(false);
//            }
//            return;
//        }
        if (ext != null && ext.length() > 0 && ext.contains("tt") && ext.contains("t") && ext.contains("entity")) {//推送
            if (!TextUtils.isEmpty(ext)) {
                ARouter.getInstance().build(ARoutUtils.ImTransferActivity).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).withString(TUIConstants.TUIOfflinePush.NOTIFICATION_EXT_KEY, ext).navigation();
//                intentAction.putExtra(TUIConstants.TUIOfflinePush.NOTIFICATION_EXT_KEY, ext);
            }
//            intentAction.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(intentAction);
            if (callBack != null) {
                callBack.onHandleOfflinePush(false);
            }
//            Type jsonType = new TypeToken<NotificationPushBean>() {
//            }.getType();
//            final NotificationPushBean jt = GsonUtils.fromJson(ext, jsonType);
//            MIPushBean entity = jt.getEntity();
//            String sender = entity.getSender();
//            V2TIMManager.getMessageManager().getC2CHistoryMessageList(sender,
//                    50,
//                    null,
//                    new V2TIMValueCallback<List<V2TIMMessage>>() {
//                        @Override
//                        public void onSuccess(List<V2TIMMessage> v2TIMMessages) {
//                            for (int i = v2TIMMessages.size() - 1; i >= 0; i--) {
//                                V2TIMMessage v2TIMMessage = v2TIMMessages.get(i);
//                                boolean read = v2TIMMessage.isRead();
//                                if (!read) {
//                                    long timestamp = v2TIMMessage.getTimestamp()  + 1;
//                                    readMessage(sender, timestamp);
//                                    break;
//                                }
//                            }
//                        }
//
//                        @Override
//                        public void onError(int i, String s) {
//
//                        }
//                    });

            return;
        }

        final OfflineMessageBean bean = OfflineMessageDispatcher.getOfflineMessageBeanFromContainer(ext);
        if (bean != null) {
            if (callBack != null) {
                callBack.onHandleOfflinePush(true);
            }
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.cancelAll();
            }

            if (bean.action == OfflineMessageBean.REDIRECT_ACTION_CHAT) {
                if (TextUtils.isEmpty(bean.sender)) {
                    return;
                }
                TUIUtils.startChat(bean.sender, bean.nickname, bean.chatType);
            }
        }
    }

    public static TUILoginConfig getLoginConfig() {
        TUILoginConfig config = new TUILoginConfig();
        if (BuildConfig.DEBUG) {
            config.setLogLevel(TUILoginConfig.TUI_LOG_DEBUG);
        }
        return config;
    }

}
